/******************************************************************************

  File Name     : face_demo.c
  Version       : Initial Draft
  Author        : tongzhilin
  Created       : 2020/8/6
  Last Modified :
  Description   : face_demo_roc, 3000*3000 images(112*112), use feature model, run
  Function List :
  History       :
  1.Date        : 2020/9/3
    Author      : tongzhilin
    Modification: Created file

******************************************************************************/
#include "image_container.h"
#include "mp_common.h"
#include "alg_struct.h"
#include "alg_api.h"

INT32 main()
{
    INT32 iSuccess = 0;
    char cLineBuff[256] = { 0 };
    FILE* pfConfig = NULL;
    FILE* pfResult = NULL;
    INT32 s32C = 0;
    INT32 s32H = 0;
    INT32 s32W = 0;
    UINT64 u64TimeStart = 0;
    ALG_INIT_PARAMS stInitParams;
    ALG_FACEFEATURE_RESULT_S stFaceFeatureRst;

#ifdef ARCH_AARCH64
    //arm64 & arm
    system("echo 3 > /proc/sys/vm/drop_caches");
#endif

    pfConfig = fopen("/home/tzl/image_db/eval_face_feature/test3000_1.txt", "r+");
    if(!pfConfig)
    {
        MP_LOG_ERR("Open image list error!\n");
        return 0;
    }
    pfResult = fopen("./feature_result_for_roc.txt", "w+");
    if(!pfResult)
    {
        MP_LOG_ERR("Open feature result file error!\n");
        return 0;
    }

    char cFaceFeatModel[] = "../../models/nnie/insightface_facefeature.wk";

    memset(&stInitParams, 0, sizeof(ALG_INIT_PARAMS));
    strcpy(stInitParams.unModel.stNnie.m_cWkFile, cFaceFeatModel);
    ALG_HANDLE pstFaceFeatureV1 = Alg_Face_Feature_Init(&stInitParams);

    TIME_START(u64TimeStart);
    while(1)
    {
        memset(cLineBuff, 0, sizeof(cLineBuff));
        if(fgets(cLineBuff, sizeof(cLineBuff), pfConfig) == NULL)
        {
            break;
        }

        if (cLineBuff[strlen(cLineBuff) - 1] == '\r' || cLineBuff[strlen(cLineBuff) - 1] == '\n')
        {
            cLineBuff[strlen(cLineBuff) - 1] = '\0';
        }
        
        s32C = Alg_Face_Get_Input_C(pstFaceFeatureV1);
        s32H = Alg_Face_Get_Input_H(pstFaceFeatureV1);
        s32W = Alg_Face_Get_Input_W(pstFaceFeatureV1);
        MP_LOG_INFO("Face feature input image c : %d, h : %d, w : %d , pic:%s, num:%d\n", s32C, s32H, s32W, cLineBuff, iSuccess);
        cv::Mat image = cv::imread(cLineBuff, cv::IMREAD_COLOR);
        if (image.empty())
        {
            MP_LOG_ERR("imread error, file name:%s\n", cLineBuff);
            return 0;
        }
        //cv::Mat image_rgb;
        //cv::cvtColor(image, image_rgb, CV_BGR2RGB);
        MP_ASSERT(s32W == image.cols && s32H == image.rows);
        ALG_IMAGE_S stAlgImage = cv2alg(image, 16);
        memset(&stFaceFeatureRst, 0, sizeof(stFaceFeatureRst));
        Alg_Face_Forward_Image(pstFaceFeatureV1, &stAlgImage, 1, &stFaceFeatureRst);

        char    cWrite_Data[4096] = {0};
        float*  f32Feature = stFaceFeatureRst.m_f32Feature;
        for (INT32 i = 0; i < stFaceFeatureRst.m_s32FeatureRealSize; i++)
        {
            sprintf(cWrite_Data + strlen(cWrite_Data),"%f ", f32Feature[i]);
        }
        //去掉最后一个空格
        INT32 last = strlen(cWrite_Data) - 1;
        cWrite_Data[last] = '\0';
        sprintf(cWrite_Data + strlen(cWrite_Data), "\n");
        fwrite(cWrite_Data, strlen(cWrite_Data), 1, pfResult);
        iSuccess++;
    }

    TIME_END(u64TimeStart, "Get feature success:%d,time:", iSuccess);

    INT32 s32FdResult = fileno(pfResult);
    fsync(s32FdResult);
    fclose(pfConfig);
    fclose(pfResult);

    return 0;
}
